package com.tanhua.server.controller;

import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.QuanziMQService;
import com.tanhua.server.vo.Movements;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @description:发布动态
 *
 */
@RequestMapping("movements")
@RestController
public class MovementsController {

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private QuanziMQService quanziMQService;

    /**
     * @description:发布动态
     *
     * @param textContent
     * @param location
     * @param longitude
     * @param latitude
     * @param multipartFile
     * @return org.springframework.http.ResponseEntity<java.lang.Void>
     */
    public ResponseEntity<Void> saveMovements(@RequestParam("textContent")String textContent,
                                              @RequestParam("location")String location,
                                              @RequestParam("longitude")String longitude,
                                              @RequestParam("latitude")String latitude,
                                              @RequestParam("imageContent")MultipartFile[] multipartFile){

        try {
            String publishId = this.movementsService.saveMovements(textContent, location, longitude, latitude, multipartFile);
            //判断
            if (StringUtils.isNotEmpty(publishId)){

                //发送消息
                this.quanziMQService.sendSavePublishMsg(1, publishId);

                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @description:查询好友的动态信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<PageResult> queryPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){
        try {
            PageResult pageResult = this.movementsService.queryUserPublishList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @description:查询推荐的动态信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("recommend")
    public ResponseEntity<PageResult> queryRecommendPublishList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                       @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize){

        try {
            PageResult pageResult = this.movementsService.queryRecommendPublishList(page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @description:点赞操作
     *
     * @param publishId
     */
    @GetMapping("/{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.likeComment(publishId);
            if (null != count){

                //发送消息
                this.quanziMQService.sendLikePublishMsg(3, publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * @description:取消点赞操作
     *
     * @param publishId
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.disLikeComment(publishId);
            if (null != count){

                //发送消息
                this.quanziMQService.sendDislikePublishMsg(6, publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @description:喜欢操作
     *
     * @param publishId
     */
    @GetMapping("/{id}/love")
    public ResponseEntity<Long> loveCommend(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.loveCommend(publishId);
            if (null != count){

                //发送消息
                this.quanziMQService.sendLovePublishMsg(4, publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @description:取消喜欢操作
     *
     * @param publishId
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity<Long> unLoveCommend(@PathVariable("id") String publishId){
        try {
            Long count = this.movementsService.unLoveCommend(publishId);
            if (null != count){

                //发送消息
                this.quanziMQService.sendDisLovePublishMsg(7, publishId);

                return ResponseEntity.ok(count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * @description:查询单条动态信息
     *
     * @param publishId
     */
    public ResponseEntity<Movements> queryMovementById(@PathVariable("id") String publishId){
        try {
            Movements movements = this.movementsService.queryMovementById(publishId);
            if (null != movements){

                //发送消息
                this.quanziMQService.sendQueryPublishMsg(2, publishId);

                return ResponseEntity.ok(movements);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
